﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：WarehousingImpl
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/14 15:13:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion

using KingerRobot.MexicoSailunMDC.Dao;
using KingerRobot.MexicoSailunWMS.Spi;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunWMS.SpiService;

public class BReportImpl : IReportService
{

    private readonly static IBakWcsTask _BakWcsTask = new BBakWcsTask();
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IBakWcsTaskCmd _BakWcsTaskCmd = new BBakWcsTaskCmd();
    private readonly static IProdSku _ProdSku = new BProdSku();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static IRcsEquipCrn _RcsEquipCrn = new BRcsEquipCrn();
    private readonly static IWcsEquipLocHold _WcsEquipLocHold = new BWcsEquipLocHold();
    private readonly static IRcsEquipCuring _RcsEquipCuring = new BRcsEquipCuring();
    private readonly static IWcsEquipLoc _WcsEquipLoc = new BWcsEquipLoc();
    public List<OutWorkCount> GetInWarehouseSummary(WareHouseTaskParm parm)
    {
        //var result = new WareHouseTaskListBean();
        //var equipsCrns = _RcsEquipCrn.GetRcsEquipCrn();
        //var equips = _RcsEquip.GetEquips();
        //var skus = _ProdSku.GetProdSkuList();
        //var materials = _ProdMaterial.GetAllMaterialList();
        //var equipsCrnIds = equipsCrns.Select(t => t.Id).ToList();
        //if (!string.IsNullOrWhiteSpace(parm.Spec))
        //{
        //    materials = materials.Where(t => t.Spec.Contains(parm.Spec)).ToList();
        //}
        //if (!string.IsNullOrWhiteSpace(parm.EquipCrnId))
        //{
        //    equipsCrnIds = equipsCrns.Where(t => t.Id == long.Parse(parm.EquipCrnId)).Select(t => t.Id).ToList();
        //}
        //if (!string.IsNullOrWhiteSpace(parm.CrnName.Trim()))//指定堆垛机名称
        //{
        //    equips = equips.Where(t => t.EquipName == parm.CrnName.Trim()).ToList();
        //}
        //var equipLocHolds = _WcsEquipLocHold.GetEquipLocHolds();//.Where(t => equipsCrnIds.Contains(t.EquipId)).ToList();
        //equipLocHolds = equipLocHolds.Join(equipsCrnIds, f => f.EquipId, s => s, (f, s) => f).ToList();

        ////K8.LSF.Client.Log(new K8.LSF.Log { Data = $"所有堆垛机{equipsCrnIds.ToJson()}" });

        //var bakTask = _BakWcsTask.GetBakWcsTasksByDateAndType(parm.StartTime, parm.EndTime, (int)EnumTaskType.Warehousing);


        //var data = bakTask.Join(equipLocHolds, f => f.TargetLoc, s => s.LocId, (f, s) => new
        //{
        //    SkuId = f.SkuId,
        //    EquipId = s.EquipId,
        //}).Join(equips, f => f.EquipId, s => s.Id, (f, s) => new
        //{
        //    SkuId = f.SkuId,
        //    EquipId = f.EquipId,
        //    EquipName = s.EquipName
        //}).Join(skus, f => f.SkuId, s => s.Id, (f, s) => new
        //{
        //    SkuId = f.SkuId,
        //    EquipName = f.EquipName,
        //    Material = s.MaterialId,
        //}).Join(materials, f => f.Material, s => s.Id, (f, s) => new
        //{
        //    SkuId = f.SkuId,
        //    EquipName = f.EquipName,
        //    Spec = s.Spec,
        //}).GroupBy(t => new { t.Spec }).Select(t => new WareHouseTaskBean
        //{
        //    Spec = t.Key.Spec,
        //    Count = t.Count()
        //}).OrderByDescending(t => t.Count).ToList();

        //var aa = _BakWcsTask.GetBakWcsTasksByDateAndType(parm.StartTime, parm.EndTime, (int)EnumTaskType.Warehousing);

        //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"查询结果 {parm.StartTime},{parm.EndTime},---{aa.ToJson()}" });

        //var result = new WareHouseTaskListBean();
        var result = _BakWcsTask.GetInWareHouseTaskBean(parm);
        return result;
    }
    public List<WareHouseTaskBean> GetOutWarehouseSummary(WareHouseTaskParm parm)
    {
        return null;

    }
    public List<OutWorkCount> GetOutWarehouseSummaryV2(WareHouseTaskParm parm)
    {
        var result = _BakWcsTask.GetOutWareHouseTaskBean(parm);
        return result;

        //var result = new List<WareHouseTaskBean>();
        //var equipsCurs = _RcsEquipCuring.GetAllEquipCurings();
        //var equips = _RcsEquip.GetEquips();
        //var skus = _ProdSku.GetProdSkuList();
        //var materials = _ProdMaterial.GetMaterialList();
        //var equipsCurIds = equipsCurs.Select(t => t.Id).ToList();
        //var equipsCrns = _RcsEquipCrn.GetRcsEquipCrn();

        //if (!string.IsNullOrWhiteSpace(parm.EquipCrnId))
        //{
        //    equipsCrns = equipsCrns.Where(t => t.Id == long.Parse(parm.EquipCrnId)).ToList();
        //}


        //var loc1 = equipsCrns.Select(t => t.CrnFork1).ToList();
        //var loc2 = equipsCrns.Select(t => t.CrnFork2).ToList();
        //var equipsCrnLocs = loc1.Union(loc2);
        //var ids = loc1.Union(loc2).ToList();
        //if (!string.IsNullOrWhiteSpace(parm.Spec))
        //{
        //    materials = materials.Where(t => t.Spec.Contains(parm.Spec)).ToList();
        //}
        //if (!string.IsNullOrWhiteSpace(parm.EquipCurId))
        //{
        //    equipsCurIds = equipsCurs.Where(t => t.Id == long.Parse(parm.EquipCurId)).Select(t => t.Id).ToList();
        //}
        //var equipLocs = _WcsEquipLoc.GetEquipLocList().Where(t => equipsCurIds.Contains(t.EquipId)).ToList();
        ////var task = _BakWcsTask.GetBakWcsTasksByDate(parm.StartTime, parm.EndTime).Where(t => t.TaskType == (int)EnumTaskType.EMS && equipsCrnLocs.Contains(t.StartEquip)) 
        ////   .ToList();
        //var query = _BakWcsTask.GetBakWcsTasksByDate(parm.StartTime, parm.EndTime).Where(t => (t.TaskType == (int)EnumTaskType.EMS || t.TaskType == (int)EnumTaskType.Exwarehouse) && ids.Contains(t.StartEquip))
        //    .Join(equipLocs, f => f.TargetLoc, s => s.LocId, (f, s) => new
        //    {
        //        SkuId = f.SkuId,
        //        EquipId = s.EquipId,
        //    }).Join(equips, f => f.EquipId, s => s.Id, (f, s) => new
        //    {
        //        SkuId = f.SkuId,
        //        EquipId = f.EquipId,
        //        EquipCode = s.EquipCode,
        //        Equip = s
        //    })
        //    .Join(equipsCurs, f => f.Equip.Id, s => s.Id, (f, s) => new
        //    {
        //        SkuId = f.SkuId,
        //        EquipId = f.EquipId,
        //        EquipCode = f.EquipCode,
        //        Indexs = s.CuringModeIndex,
        //        Signs = s.CuringSign
        //    })
        //    .Join(skus, f => f.SkuId, s => s.Id, (f, s) => new
        //    {
        //        SkuId = f.SkuId,
        //        EquipCode = f.EquipCode,
        //        Material = s.MaterialId,
        //        Indexs = f.Indexs,
        //        Signs = f.Signs,
        //    })
        //    .Join(materials, f => f.Material, s => s.Id, (f, s) => new
        //    {
        //        SkuId = f.SkuId,
        //        EquipCode = f.EquipCode,
        //        Indexs = f.Indexs,
        //        LeftRightFlag = f.Signs is not null ? (f.Signs == 1 ? (f.Indexs == 1 ? "R" : "L") : (f.Indexs == 1 ? "L" : "R")) : null,//左右膜
        //        Spec = s.Spec
        //    }).ToList();

        //if (!string.IsNullOrWhiteSpace(parm.CuringCode))//指定硫化机编号
        //{
        //    query = query.Where(t => t.EquipCode.Contains(parm.CuringCode.Trim())).ToList();
        //}

        //if (!string.IsNullOrWhiteSpace(parm.Spec))//指定硫化机编号
        //{
        //    query = query.Where(t => t.Spec == parm.Spec.Trim()).ToList();
        //}


        //result = query.GroupBy(t => new { t.EquipCode, t.Spec, t.LeftRightFlag }).Select(t => new WareHouseTaskBean
        //{
        //    EquipCode = t.Key.EquipCode,
        //    LeftRightFlag = t.Key.LeftRightFlag,
        //    Spec = t.Key.Spec,
        //    Count = t.Count()
        //}).OrderBy(t => t.EquipCode).ToList();


        //var result = _BakWcsTask.GetInWareHouseTaskBean(parm);



        //return result;


        //return null;
    }



    public List<OutWorkCount> GetOutWarehouseSummaryV3(WareHouseTaskParm parm)
    {
        var result = new List<OutWorkCount>();

        var db = Db.Default;
        {
            var query = db.BakWmsOutOrderSummarys
                .Join(db.ProdMaterials, a => a.MaterialId, b => b.Id, (a, b) => new { summary = a, mater = b })
                .Join(db.RcsLocations, a => a.summary.StartLoc, b => b.Id, (a, b) => new { summary = a.summary, mater = a.mater, loc = b })
               //.Where(T => true);
               .Where(T => T.summary.CreateTime >= parm.StartTime && T.summary.CreateTime <= parm.EndTime);

            if (!string.IsNullOrEmpty(parm.Spec))
            {
                query = query.Where(T => T.mater.Spec == parm.Spec);
            }

            if (!string.IsNullOrEmpty(parm.CrnName))
            {
                query = query.Where(T => T.loc.LocName.Contains(parm.CrnName));
            }

            result = query.GroupBy(t => new { t.mater.Spec }).Select(t => new OutWorkCount
            {
                Spec = t.Key.Spec,
                Count = t.Count()
            }).ToList();
        }

        return result;
    }



    public List<WareHouseCountBean> GetInWarehouseCount(WareHouseParm parm)
    {
        var result = new List<WareHouseCountBean>();
        var equips = _RcsEquip.GetEquips();
        result = _BakWcsTaskCmd.GetBakWcsTaskCmdsByDate(parm.StartTime, parm.EndTime).Where(t => t.TaskType == (int)EnumTaskType.Warehousing && t.CmdStatus == (int)EnumTaskCmdStatus.Finish)
           .GroupBy(t => t.EquipId).Select(t => new
           {
               StartTime = parm.StartTime,
               EndTime = parm.EndTime,
               EquipId = t.Key,
               Count = t.Count()
           }).Join(equips, f => f.EquipId, s => s.Id, (f, s) => new WareHouseCountBean
           {
               StartTime = f.StartTime,
               EndTime = f.EndTime,
               EquipName = s.EquipName,
               Count = f.Count
           }).ToList();

        return result;
    }
    public List<WareHouseCountBean> GetOutWarehouseCount(WareHouseParm parm)
    {
        var result = new List<WareHouseCountBean>();
        var equips = _RcsEquip.GetEquips();
        result = _BakWcsTaskCmd.GetBakWcsTaskCmdsByDate(parm.StartTime, parm.EndTime).Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse && t.CmdStatus == (int)EnumTaskCmdStatus.Finish)
           .GroupBy(t => t.EquipId).Select(t => new
           {
               StartTime = parm.StartTime,
               EndTime = parm.EndTime,
               EquipId = t.Key,
               Count = t.Count()
           }).Join(equips, f => f.EquipId, s => s.Id, (f, s) => new WareHouseCountBean
           {
               StartTime = f.StartTime,
               EndTime = f.EndTime,
               EquipName = s.EquipName,
               Count = f.Count
           }).ToList();

        return result;
    }
    public List<WareHouseDetailBean> GetInWarehouseDetail(WareHouseParm parm)
    {
        var result = new List<WareHouseDetailBean>();
        var prodSkus = _ProdSku.GetProdSkuList();
        var proMaterial = _ProdMaterial.GetMaterialList();
        result = _BakWcsTask.GetBakWcsTasksByDate(parm.StartTime, parm.EndTime).Where(t => t.TaskType == (int)EnumTaskType.Warehousing && t.TaskStatus == (int)EnumTaskCmdStatus.Finish)
         .Select(t => new
         {
             Createtime = t.CreateTime,
             SkuId = t.SkuId
         }).Join(prodSkus, f => f.SkuId, s => s.Id, (f, s) => new
         {
             CreateTime = f.Createtime.GetValueOrDefault(),
             BarCode = s.SkuCode,
             MaterialId = s.MaterialId
         }).Join(proMaterial, f => f.MaterialId, s => s.Id, (f, s) =>
           new WareHouseDetailBean
           {
               CreateTime = f.CreateTime,
               BarCode = f.BarCode,
               MaterialCode = s.MaterialCode,
               MaterialName = s.MaterialName
           }
          ).ToList();

        return result;
    }
    public List<WareHouseDetailBean> GetOutWarehouseDetail(WareHouseParm parm)
    {
        var result = new List<WareHouseDetailBean>();
        var prodSkus = _ProdSku.GetProdSkuList();
        var proMaterial = _ProdMaterial.GetMaterialList();
        result = _BakWcsTask.GetBakWcsTasksByDate(parm.StartTime, parm.EndTime).Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse && t.TaskStatus == (int)EnumTaskCmdStatus.Finish)
         .Select(t => new
         {
             Createtime = t.CreateTime,
             SkuId = t.SkuId
         }).Join(prodSkus, f => f.SkuId, s => s.Id, (f, s) => new
         {
             CreateTime = f.Createtime.GetValueOrDefault(),
             BarCode = s.SkuCode,
             MaterialId = s.MaterialId
         }).Join(proMaterial, f => f.MaterialId, s => s.Id, (f, s) =>
           new WareHouseDetailBean
           {
               CreateTime = f.CreateTime,
               BarCode = f.BarCode,
               MaterialCode = s.MaterialCode,
               MaterialName = s.MaterialName
           }
          ).ToList();

        return result;
    }

    public List<WareHouseTaskBean> GetCrnOutWareHouse(CrnOutWareHouseParam param)
    {
        var result = new List<WareHouseTaskBean>();
        var equipsCrns = _RcsEquipCrn.GetRcsEquipCrn();
        var equips = _RcsEquip.GetEquips();
        var skus = _ProdSku.GetProdSkuList();
        var materials = _ProdMaterial.GetAllMaterialList();
        var equipsCrnIds = equipsCrns.Select(t => t.Id).ToList();
        if (!string.IsNullOrWhiteSpace(param.Spec))//指定规格
        {
            materials = materials.Where(t => t.Spec.Contains(param.Spec)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(param.CrnName))//指定堆垛机名称
        {
            equips = equips.Where(t => t.EquipName == param.CrnName.Trim()).ToList();
        }
        var equipLocHolds = _WcsEquipLocHold.GetEquipLocHolds().Where(t => equipsCrnIds.Contains(t.EquipId)).ToList();

        K8.LSF.Client.Log(new K8.LSF.Log { Data = $"所有堆垛机{equipsCrnIds.ToJson()}" });

        result = _BakWcsTask.GetBakWcsTasksByDate(param.StartTime, param.EndTime).Where(t => t.TaskType == (int)EnumTaskType.EMS || t.TaskType == (int)EnumTaskType.Exwarehouse)
            .Join(equipLocHolds, f => f.StartLoc, s => s.LocId, (f, s) => new
            {
                SkuId = f.SkuId,
                EquipId = s.EquipId,
            }).Join(equips, f => f.EquipId, s => s.Id, (f, s) => new
            {
                SkuId = f.SkuId,
                EquipId = f.EquipId,
                EquipName = s.EquipName
            }).Join(skus, f => f.SkuId, s => s.Id, (f, s) => new
            {
                SkuId = f.SkuId,
                EquipName = f.EquipName,
                Material = s.MaterialId,
            }).Join(materials, f => f.Material, s => s.Id, (f, s) => new
            {
                SkuId = f.SkuId,
                EquipName = f.EquipName,
                Spec = s.Spec,
            }).GroupBy(t => new { t.Spec }).Select(t => new WareHouseTaskBean
            {
                Spec = t.Key.Spec,
                Count = t.Count()
            }).ToList();

        var aa = _BakWcsTask.GetBakWcsTasksByDate(param.StartTime, param.EndTime).Where(t => t.TaskType == (int)EnumTaskType.Warehousing);

        K8.LSF.Client.Log(new K8.LSF.Log { Data = $"查询结果 {param.StartTime},{param.EndTime},---{aa.ToJson()}" });

        return result;
    }
    public List<InOutCountBean> GetInOutCount(InOutCountParam param)
    {
        if (!(param.StartTime is not null && param.StartTime > DateTime.MinValue)) param.StartTime = DateTime.Now.Date;
        if (!(param.EndTime is not null && param.EndTime > DateTime.MinValue)) param.EndTime = DateTime.Now;

        return _RcsEquipCrn.GetInOutCount(param);
    }
}



